package org.jeecg.modules.demo.znnqjc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.demo.znnqjc.entity.ZwglZwgc;
import org.jeecg.modules.demo.znnqjc.entity.ZwglZwgcfb;
import org.jeecg.modules.demo.znnqjc.mapper.ZwglZwgcMapper;
import org.jeecg.modules.demo.znnqjc.mapper.ZwglZwgcfbMapper;
import org.jeecg.modules.demo.znnqjc.service.IZwglZwgcfbService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 作物估产附表
 * @Author: jeecg-boot
 * @Date:   2023-04-20
 * @Version: V1.0
 */
@Service
public class ZwglZwgcfbServiceImpl extends ServiceImpl<ZwglZwgcfbMapper, ZwglZwgcfb> implements IZwglZwgcfbService {
	
	@Autowired
	private ZwglZwgcfbMapper zwglZwgcfbMapper;
	@Autowired
	private ZwglZwgcMapper z;
	
	@Override
	public List<ZwglZwgcfb> selectByMainId(String mainId) {
		return zwglZwgcfbMapper.selectByMainId(mainId);
	}


	@Override
	public List<Map<String, Object>> getsum(){
		List<Map<String, Object>> aggregatedResults = new ArrayList<>();
		QueryWrapper<ZwglZwgc> queryWrapper = new QueryWrapper<>();
		List<ZwglZwgc> mainTableEntities = z.selectList(queryWrapper);
		ZwglZwgc zwglZwgc = new ZwglZwgc();
		for (ZwglZwgc entity : mainTableEntities) {
			String wj = entity.getId();
			List<Map<String, Object>> results = zwglZwgcfbMapper.selectTotalSumGroupedByDj(wj);
			System.out.println(results);
			for (Map<String, Object> result : results) {
				if ("等级1".equals(result.get("dj"))){
					zwglZwgc.setDj1(String.valueOf(result.get("total_sz")));
					zwglZwgc.setId(String.valueOf(result.get("wj")));
				} else if ("等级2".equals(result.get("dj"))) {
					zwglZwgc.setDj2(String.valueOf(result.get("total_sz")));
					zwglZwgc.setId(String.valueOf(result.get("wj")));
				}else if ("等级3".equals(result.get("dj"))) {
					zwglZwgc.setDj3(String.valueOf(result.get("total_sz")));
					zwglZwgc.setId(String.valueOf(result.get("wj")));
				}else if ("等级4".equals(result.get("dj"))) {
					zwglZwgc.setDj4(String.valueOf(result.get("total_sz")));
					zwglZwgc.setId(String.valueOf(result.get("wj")));
				}else if ("等级5".equals(result.get("dj"))) {
					zwglZwgc.setDj5(String.valueOf(result.get("total_sz")));
					zwglZwgc.setId(String.valueOf(result.get("wj")));
				}else if ("等级6".equals(result.get("dj"))) {
					zwglZwgc.setDj6(String.valueOf(result.get("total_sz")));
					zwglZwgc.setId(String.valueOf(result.get("wj")));
				}else if ("等级7".equals(result.get("dj"))) {
					zwglZwgc.setDj7(String.valueOf(result.get("total_sz")));
					zwglZwgc.setId(String.valueOf(result.get("wj")));
				}else if ("等级8".equals(result.get("dj"))) {
					zwglZwgc.setDj8(String.valueOf(result.get("total_sz")));
					zwglZwgc.setId(String.valueOf(result.get("wj")));
				}
				z.updateById(zwglZwgc);
			}
		}
		return aggregatedResults;
	}
}
